from rest_framework import serializers

from apps.users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        # 选择模型类
        model = User
        # 指定字段
        fields = ['id', 'username', 'mobile', 'email']
        # fields = ['__all__']


class UserAddSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'password', 'mobile', 'email']
        # 给指定字段加约束
        extra_kwages = {
            # 字段名: { 选项名： value}
            'username': {'max_length': 20, 'min_length': 8},
            'password': {'max_length': 20, 'min_length': 8, 'write_only': True}
        }

    # create 方法不满足我们的需要，需要重写
    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        return user
